package com.hb.trans.mvp.ui.fragment


import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.basiclib.base.BaseFragment
import com.hb.trans.R
import com.hb.trans.bean.New
import com.hb.trans.global.ACTIVITY_NEW_DETAIL
import com.hb.trans.mvp.contract.NewListContract
import com.hb.trans.mvp.presenter.NewListPresenter
import com.hb.trans.mvp.ui.adapter.NewListAdapter
import dagger.android.support.AndroidSupportInjection
import kotlinx.android.synthetic.main.fragment_new_list.*
import javax.inject.Inject

/**
 * 资讯分类
 */
class NewListFragment : BaseFragment<NewListPresenter>(), NewListContract.View {
    override fun setViewId(): Int {
        return R.layout.fragment_new_list
    }

    override fun doOnInvisible() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun doOnVisible() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    @Inject
    lateinit var mPresenter: NewListPresenter

//    lateinit var rootView: View
    lateinit var mAdapter: NewListAdapter
    var labelId: String? = null
    var isRefresh: Boolean = true

    companion object {
        fun newInstance(labelId: String): NewListFragment {
            val fragment = NewListFragment()
            val bundle = Bundle()
            bundle.putString("labelId", labelId)
            fragment.arguments = bundle
            return fragment
        }
    }

//    override fun initView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
//        rootView = inflater.inflate(R.layout.fragment_new_list, container, false)
//        return rootView
//    }

    override fun onFragmentInject() {
        AndroidSupportInjection.inject(this)
    }

    override fun attachView() {
        mPresenter.attachView(this)
    }

    override fun initView() {
        newListRv.apply {
            layoutManager = androidx.recyclerview.widget.LinearLayoutManager(mContext)
            mAdapter = NewListAdapter(R.layout.item_new)
            adapter = mAdapter
        }
    }

    override fun initData() {
        labelId = arguments?.getString("labelId")
        if (!labelId.isNullOrBlank()) {
            mPresenter.getNewList(labelId!!, isRefresh)
        }
    }

    override fun setListener() {
        mAdapter.setOnItemClickListener { adapter, view, position ->
            val new = adapter.getItem(position) as New
            ARouter.getInstance().build(ACTIVITY_NEW_DETAIL)
                .withString("articleId", new.id)
                .navigation()
        }
    }

    override fun setupData(news: MutableList<New>?) {
        news?.let {
            mAdapter.setNewData(news)
        }
    }
}
